package com.wcbtest.oshi.controller;

import cn.hutool.system.oshi.OshiUtil;
import com.alibaba.fastjson.JSONObject;
import com.wcbtest.oshi.domain.*;
import com.wcbtest.oshi.utils.OshiUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.VirtualMemory;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;

import java.util.List;

@RestController
@Api(tags = "硬件信息基础接口")
@RequestMapping("/oshi")
public class OshiController {


    @ApiOperation(value = "基本信息")
    @GetMapping("/sysInfo")
    public Object sysInfo() {
        OshiUtils oshiUtils = new OshiUtils();
        oshiUtils.initData();
        return oshiUtils;
    }
    @ApiOperation(value = "文件系统")
    @GetMapping("/fileSys")
    public Object FileSys() {
        return OshiUtils.getOsDiskFile();
    }

    @ApiOperation(value = "硬件硬盘")
    @GetMapping("/diskStores")
    public static Object diskStores() {
        return OshiUtils.getDiskStores();
    }




    @ApiOperation(value = "获取进程信息")
    @GetMapping("/proc")
    public Object jin(Proc proc) {
        OshiUtils oshiUtils = new OshiUtils();
        return oshiUtils.getProcessInfo(proc);
    }

    @ApiOperation(value = "杀死进程")
    @GetMapping("/kill")
    public Object kill(int pid) {
        OshiUtils oshiUtils = new OshiUtils();
        boolean b = oshiUtils.killProcess(pid);
        return b;
    }








    @ApiOperation(value = "获取系统信息")
    @GetMapping("/baseinfo")
    public static Object baseinfo() {
        JSONObject jsonObject = new JSONObject();
        OperatingSystem os = OshiUtil.getOs();

        CentralProcessor processor = OshiUtil.getProcessor();
        jsonObject.put("osCpuCores", processor.getLogicalProcessorCount()); // Get the number of logical CPUs available for processing.
        GlobalMemory globalMemory = OshiUtil.getMemory();
        VirtualMemory virtualMemory = globalMemory.getVirtualMemory();

        jsonObject.put("osSwapTotal", virtualMemory.getSwapTotal());         // The current size of the paging/swap file(s), in bytes.
        jsonObject.put("osVirtualMax", virtualMemory.getVirtualMax()); // The maximum memory that can be committed by the system without extending the paging file(s), in bytes.
        FileSystem fileSystem = os.getFileSystem();
        List<OSFileStore> fileStores = fileSystem.getFileStores();
        long total = fileStores.stream().mapToLong(OSFileStore::getTotalSpace).sum();
        jsonObject.put("osFileStoreTotal", total); //         Get file stores on this machine Instantiates a list of OSFileStore objects, representing a storage pool, device, partition, volume, concrete file system or other implementation specific means of file storage.


        OSSys OSSysInfo = OshiUtils.getSysInfo();
        Cpu cpuInfo = OshiUtils.getCpuInfo();
        Jvm jvmInfo = OshiUtils.getJvmInfo();
        Mem memInfo = OshiUtils.getMemInfo();
        jsonObject.put("sysInfo", OSSysInfo);
        jsonObject.put("cpuInfo", cpuInfo);
        jsonObject.put("jvmInfo", jvmInfo);
        jsonObject.put("memInfo", memInfo);




        return jsonObject;
    }
}
